package transientTables;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import tools.Configuration;


public class DBLayer {
	java.sql.Connection connection = null;
	String connection_string = "";
	
	int queryCounter = 0;
	/**
	 * Indicate whether you are going to execute a query while another ResultSet is open
	 * setting false will increase memory efficiency
	 */
	public boolean nestedQueries = true;
	
	java.sql.Connection getConnection() throws SQLException
	{
		if(queryCounter>1000 && !nestedQueries)
		{
			connection.close();
			queryCounter = 0;
		}
		if(connection == null || connection.isClosed())
			connection = 
				java.sql.DriverManager
				.getConnection(connection_string);
		connection.clearWarnings();
		return connection;
	}
	public DBLayer() {
		connection_string = "jdbc:mysql://localhost/" + Configuration.getValue("semeval_database_name")
			+ "?user=" + Configuration.getValue("database_username")+ "&password="+
			Configuration.getValue("database_password");
		try {

			Class.forName("com.mysql.jdbc.Driver").newInstance();
			batch_statement = java.sql.DriverManager
			.getConnection(connection_string).createStatement();

			
		} catch (Exception e) {
			System.out.println(e);
		}
	}
	
	/**
	 * Execute a "Select" query
	 */
	public ResultSet executeReader(String query) {
		queryCounter++;
		ResultSet r = null;
		try {
			Statement s = getConnection().createStatement();
			r = s.executeQuery(query);
			
		} catch (Exception e) {
			System.out.println(e);
		}
		return r;
	}
	Statement batch_statement;
	
	public void executeNonQuery(String query) throws SQLException {
		if(Configuration.RunSQLBatchMode)
		{
			//System.out.println("Adding to batch: "+query);
			batch_statement.addBatch(query);
						
		}else{
			try {
			//	System.out.println("Running: "+query);
				queryCounter++;
					Statement s = getConnection().createStatement();
					s.executeUpdate(query);
					s.close();
			} catch (Exception e) {
				System.out.println(e);
			}
		}
	}
	public Integer executeOneIntegerQuery(String query) throws SQLException {
		queryCounter++;
		Integer result = -1;
		ResultSet r = null;
		try {
			Statement s = getConnection().createStatement();
			r = s.executeQuery(query);

		} catch (Exception e) {
			System.out.println(e);
		}
		if(r.first())
		{
			result = r.getInt(1);
		}
		r.getStatement().close();
		r.close();
		return result;
	}
	/*
	 * Return null if not found
	 */
	public String executeOneStringQuery(String query) throws SQLException {
		queryCounter++;
		String result = null;
		ResultSet r = null;
		try {
			Statement s = getConnection().createStatement();
			r = s.executeQuery(query);

		} catch (Exception e) {
			System.out.println(e);
		}
		if(r.first())
		{
			result = r.getString(1);
		}
		r.getStatement().close();
		r.close();
		return result;
	}
	public void runBatch() {
		try {
			queryCounter++;
			//System.out.println("running batch ...");
			batch_statement.executeBatch();
			batch_statement.clearBatch();
			//System.out.println("batch done.");
		} catch (Exception e) {
			System.out.println(e);
		}
		
	}
	public Double executeOneDoubleQuery(String selectQuery) throws SQLException {
		queryCounter++;
		Double result = -1.0;
		ResultSet r = null;
		try {
			Statement s = getConnection().createStatement();
			r = s.executeQuery(selectQuery);

		} catch (Exception e) {
			System.out.println(e);
		}
		if(r.first())
		{
			result = r.getDouble(1);
		}
		r.getStatement().close();
		r.close();
		return result;
	}
}
